﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section style{
    <link href="@Url.Content("~/css/plat/blue.css")" rel="stylesheet">
}
<section class="content-header">
    <h1>
        用户管理
        <small></small>
    </h1>
    <ol class="breadcrumb"></ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header">
                    <div class="form-inline">
                        <div class="form-group" style="display:none">
                            <label>用户名</label>
                            <input type="text" class="form-control" style="width:100px;" maxlength="20" id="userName" autocomplete="off" />
                        </div>
                        <div class="form-group">
                            <button type="button" class="btn btn-success btn-sm" id="btnDelete">批量删除</button>
                            <button type="button" class="btn btn-success btn-sm" id="btnAdd">添加用户</button>
                            <button type="button" style="display:none" class="btn btn-success btn-sm" id="btnSearch">查询</button>
                        </div>


                        <h3 class="box-title"></h3>
                    </div>
                </div>
                <div id="datatable">

                </div>
                <!-- /.box-header -->
                <div class="box-footer clearfix ">
                    <ul id="pagination" class="pagination pagination-sm no-margin pull-right"></ul>
                </div>
            </div>
        </div>
    </div>
</section>
@section scriptsBottom{
    <script src="@Url.Content("~/js/icheck.min.js")"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnSearch").click(function () {
                pageIndex = 1;
                search();
            });
            $("#btnAdd").click(function () {
                edit('添加用户');
            });
            $("#btnDelete").click(function () {
                batchDel();
            });

            //分类
            search();
        });

        //页码
        var pageIndex = 1;
        //查询
        function search() {
            var data = {
                name: $("#userName").val(),
                rows: 10,
                page: pageIndex
            }
            $("#datatable").load('@Url.Action("ParUserList", "User")', data, function (data) {
                document.getElementById('datatable').innerHTML = data;
                $('input').iCheck({
                    checkboxClass: 'icheckbox_flat-blue'
                });
                initCheckbox();
                //alert($("#dataView").data("count"));
                //分页显示
                $('#pagination').twbsPagination("destroy");
                $('#pagination').twbsPagination({
                    totalItems: $("#dataView").data("count"),
                    visiblePages: 10,
                    pageSize: 10,
                    startPage: parseInt(pageIndex),
                    onPageClick: function (event, page) {
                        pageIndex = page;
                        search();
                    }
                });
            })
        }

        //修改
        function edit(title, id) {
            var tl = title == '' ? '修改用户' : title;
            var url = '@Url.Action("EditUser", "User")'
            url += id ? "?id=" + id : "";
            BMCW.showTopLayer(tl, url, 830, 603);
        }

        //删除
        function del(id) {
            var url = '@Url.Action("DeleteUser", "User")';
            BMCW.DataConfirm("", function () {
                BMCW.ajax({
                    url: url,
                    type: "POST",
                    data: {
                        id: id
                    },
                    success: function (o) {
                        console.log(o);
                        if (o.status == 0) {
                            BMCW.msg("删除成功", function () {
                                search();
                            });

                        } else {
                            BMCW.alert("删除失败");
                        }
                    },
                    error: function (o) {
                        console.log(o);
                    }
                });
            })
        }

        // 重置密码
        function reset(id) {
            var tl = '重置密码';
            var url = '@Url.Action("EditPassword", "User")'
            url += id ? "?id=" + id : "";
            BMCW.showTopLayer(tl, url, 500, 300);
        }

        //批量删除
        function batchDel() {
            var selStr = "";
            var $sel = $("#dataView").find("input[type='checkbox']:checked");
            if ($sel.length > 0) {
                $sel.each(function () {
                    selStr += $(this).val() + ",";
                });
                selStr = selStr.substring(0, selStr.length - 1);
                console.log(selStr);

                var url = '@Url.Action("BatchDeleteUser", "User")';
                BMCW.DataConfirm("", function () {
                    BMCW.ajax({
                        url: url,
                        type: "Post",
                        data: {
                            "ids": selStr
                        },
                        success: function (o) {
                            console.log(o);
                            if (o.status == 0) {
                                BMCW.msg("删除成功", function () {
                                    search();
                                });

                            } else {
                                BMCW.alert("删除失败");
                            }
                        },
                        error: function (o) {
                            console.log(o);
                        }
                    });
                })
            }
        }

        //初始化全选和全部取消操作
        function initCheckbox() {
            //全选
            $('#selPageAll').on('ifChecked', function (event) {
                $('input').iCheck('check');
            });
            //反选
            $('#selPageAll').on('ifUnchecked', function (event) {
                $('input').iCheck('uncheck');
            });
        }

        function configCity(id) {
            var url = '@Url.Action("ConfigUserCities", "User")';
            url += "?id="+id;
            var title = id == null || id == "" ? "添加城市" : "编辑城市";
            BMCW.showTopLayer(title, url, 1200, 600);
        }

    </script>
}